﻿@using Nop.Core.Infrastructure
@using Nop.Plugin.Pricing.PreciousMetals.Models
@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@{
    Layout = "~/Administration/Views/Shared/_AdminLayout.cshtml";
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}
@model GridModel<PreciousMetalsGridDetailModel>
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
            @T("Admin.Catalog.Products.PreciousMetalPricing.ListEdit")
        </div>
        <div class="options">
            <a href="@Url.Action("BulkEdit", "Plugins/PreciousMetals/Admin")" class="t-button">@T("Admin.Catalog.PreciousMetal.BulkEdit")</a>
        </div>
    </div>    
    <table class="adminContent">
        <tr>
            <td>
                <script type="text/javascript">
                    var enumMetalValues = ["", "Gold", "Silver", "Platinum", "Palladium", "Rhodium"];
                    var enumQuoteValues = ["", "Bid", "Ask", "Low", "High"];
                    var enumTierPriceValues = ["", "DoNotUse", "Percentage", "PriceDiscount"];
                    var enumMathValues = ["", "Multiply First Then Add", "Add First Then Multiply"];
                    var enumPriceRoundingValues = ["", "None","Up","Down"];
               
                    @Html.Raw(((string)ViewData["WeightUnitsJS"]))
                    
                </script>
                @(Html.Telerik().Grid<PreciousMetalsGridDetailModel>()
                    .Name("preciousprice-grid")
                    .DataKeys(keys => keys.Add(pv => pv.Id))
                    .DataBinding(dataBinding =>
                        dataBinding.Ajax()
                        .Select("BulkEditSelect", "Plugins/PreciousMetals/Admin")
                        .Update("BulkEditSave", "Plugins/PreciousMetals/Admin")
                    )
                    .Columns(columns =>
                    {
                        //columns.Bound(x => x.Id)
                        //    .Width(50)
                        //    .Filterable(false);

                        columns.Bound(x => x.ProductVariantTitle).ReadOnly().Width(150).Filterable(false);
                        columns.Bound(pv => pv.MetalType).ClientTemplate("<#= enumMetalValues[MetalType] #>").Width(80);

                        columns.Bound(pv => pv.QuoteType).ClientTemplate("<#= enumQuoteValues[QuoteType] #>").Width(80);

                        columns.Bound(pv => pv.Weight).Width(80);
                        columns.Bound(pv => pv.WeightId).ClientTemplate("<#= weightUnits[WeightId] #>").Width(80);

                        columns.Bound(pv => pv.FlatMarkup).Width(80);
                        columns.Bound(pv => pv.PercentMarkup).Width(80);

                        columns.Bound(pv => pv.TierPriceType).ClientTemplate("<#= enumTierPriceValues[TierPriceType] #>").Width(80);

                        columns.Bound(pv => pv.LowerAmount).Width(80);
                        columns.Bound(pv => pv.MathType).ClientTemplate("<#= enumMathValues[MathType] #>").Width(80);

                        columns.Bound(pv => pv.PriceRoundingType).ClientTemplate("<#= enumPriceRoundingValues[PriceRoundingType] #>").Width(80);
                        columns.Bound(pv => pv.PriceRounding).Width(80);
                        
                        columns.Bound(pv => pv.CurrentPrice).Format("{0:0.00}");

                    })
                    .Editable(editing => editing.Mode(GridEditMode.InCell))
                    .ToolBar(commands => commands.SubmitChanges())
                    .ClientEvents(events => events.OnEdit("onPreciousMetalEdit"))
                    .Pageable(settings => settings.Total(Model.Total).PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .DataBinding(dataBinding => dataBinding.Ajax().Select("PricingList", "Plugins/PreciousMetals/Admin")))
                <script type="text/javascript">

                    function onPreciousMetalEdit(e) {
                        var element = $(this).data("tGrid");
                        var cell = e.cell;

                        if ($("#MetalType_MetalType").length > 0) {
                            $("#MetalType_MetalType").val(e.dataItem['MetalType']);
                        }

                        if ($("#QuoteType_QuoteType").length > 0) {
                            $("#QuoteType_QuoteType").val(e.dataItem['QuoteType']);
                        }

                        if ($("#TierPriceType_TierPriceType").length > 0) {
                            $("#TierPriceType_TierPriceType").val(e.dataItem['TierPriceType']);
                        }

                        if ($("#MathType_MathType").length > 0) {
                            $("#MathType_MathType").val(e.dataItem['MathType']);
                        }

                        if ($("#WeightId_WeightId").length > 0) {
                            $("#WeightId_WeightId").val(e.dataItem['WeightId']);
                        }

                        if ($("#PriceRoundingType_PriceRoundingType").length > 0) {
                            $("#PriceRoundingType_PriceRoundingType").val(e.dataItem['PriceRoundingType']);
                        }
                    }
                </script>
            </td>
        </tr>
    </table>
       
}